package com.company.DivideandConquer;

import java.util.List;
import java.util.Stack;

public class DiffWayToCompute {

    public static class Operate {

        public Operate beforeOperator;
        public Operate afterOperator;
        public char operateType = ' ';

        int value = 0;

        public Operate(Operate beforeOperator, Operate afterOperator, char operateType, int value) {
            this.beforeOperator = beforeOperator;
            this.afterOperator = afterOperator;
            this.operateType = operateType;
            this.value = value;
        }

        public static Operate fromExpress(String express) {
            //这是个递归方法由 外向内

            if (!express.contains("(")
                    && !express.contains(")")
                    && !express.contains("+")
                    && !express.contains("-")
                    && !express.contains("*")) {
                int value = Integer.parseInt(express);
                return new Operate(null, null, ' ', value);
            }
            // 首先找到最外层的括号
            int left = 0;
            int right = express.length() - 1;

            while (left < right) {
                if (express.charAt(left) != '(') {
                    left++;
                    continue;
                }

                if (express.charAt(right) != ')') {
                    right--;
                    continue;
                }
                break;
            }
            if (left == 0 && right == express.length() - 1) {//如果是最外层一个多余的括号 去除括号
                return Operate.fromExpress(express.substring(1, express.length() - 1));
            }


            //如果是有效括号 找第一个操作符
            int desirePosition = left - 1;
            if (desirePosition > 0) {//操作符在 右边
                if (express.charAt(desirePosition) == '+' ||
                        express.charAt(desirePosition) == '-' ||
                        express.charAt(desirePosition) == '*') {
                    String frontExpress = express.substring(0, desirePosition-1);
                    String afterExpress = express.substring(desirePosition);

                    return new Operate(Operate.fromExpress(frontExpress),Operate.fromExpress(afterExpress),express.charAt(desirePosition),0);
                }else {
                    return null;
                }
            }else {//操作符在左边
                desirePosition = right+1;
                if (express.charAt(desirePosition) == '+' ||
                        express.charAt(desirePosition) == '-' ||
                        express.charAt(desirePosition) == '*') {
                    String frontExpress = express.substring(0, desirePosition);
                    String afterExpress = express.substring(desirePosition+1);
                    return new Operate(Operate.fromExpress(frontExpress),Operate.fromExpress(afterExpress),express.charAt(desirePosition),0);
                }else {
                    return null;
                }
            }
        }

        @Override
        public String toString() {
            if (beforeOperator == null && afterOperator == null) {
                return value + "";
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("(");
            if (beforeOperator != null) {
                stringBuilder.append(beforeOperator.toString());
            }
            stringBuilder.append(operateType);
            if (afterOperator != null) {
                stringBuilder.append(afterOperator.toString());
            }
            stringBuilder.append(")");
            return stringBuilder.toString();
        }

        public int getResult() {
            switch (operateType) {
                case '+':
                    return beforeOperator.getResult() + afterOperator.getResult();
                case '-':
                    return beforeOperator.getResult() - afterOperator.getResult();
                case '*':
                    return beforeOperator.getResult() * afterOperator.getResult();
                default:
                    return value;
            }
        }
    }


    public List<Integer> diffWaysToCompute(String expression) {

        return null;
    }

}
